<?xml version='1.0' encoding='utf-8' ?>
<!--
	Author: Nont Banditwong
	File: gdacs2edxml.xsl
	Date: 23 April 2007
	Purpose: GDACS to EDXL conversion
-->
<xsl:stylesheet version="1.0" xmlns="urn:oasis:names:tc:emergency:EDXL:DE:1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://exslt.org/dates-and-times"
	extension-element-prefixes="date" xmlns:cap="urn:oasis:names:tc:emergency:cap:1.1" xmlns:gdas="http://www.gdacs.org" 
	xmlns:asgard="http://asgard.jrc.it" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:geo="http://www.w3.org/2003/01/geo/"
	xmlns:glide="http://glidenumber.net" xmlns:icbm="http://postneo.com/icbm/">

	<xsl:output method="xml" indent="no" encoding="UTF-8" />

	<xsl:template match="rss">
		<EDXLDistribution xmlns="urn:oasis:names:tc:emergency:EDXL:DE:1.0">
			<distributionID>asgard:UTOR</distributionID>			
			<xsl:apply-templates select="channel/webMaster"/>
			<xsl:apply-templates select="channel/pubDate"/>
			<xsl:apply-templates select="channel/item[1]/cap:alert/cap:status"/>
			<xsl:apply-templates select="channel/item[1]/cap:alert/cap:msgType"/>
			<combinedConfidentiality>UNCLASSIFIED AND NOT SENSITIVE</combinedConfidentiality>
			<xsl:apply-templates select="channel/item/cap:alert/cap:info/cap:area"/>
			<xsl:apply-templates select="channel/item"/>
		</EDXLDistribution>
	</xsl:template>

	<xsl:template match="rss/channel/webMaster">		
		<senderID><xsl:value-of select="."/></senderID>
	</xsl:template>

  	<xsl:template match="rss/channel/pubDate">
		<dateTimeSent>
			<xsl:call-template name="FormatDate">
				<xsl:with-param name="DateTime" select="."/>
			</xsl:call-template>
		</dateTimeSent>
  	</xsl:template>

  	<xsl:template match="rss/channel/item/cap:alert/cap:status">
			<distributionStatus>
				<xsl:choose>
						<xsl:when test="text()='Actual'">Actual</xsl:when>
						<xsl:when test="text()='Exercise'">Exercise</xsl:when>
						<xsl:when test="text()='System'">System</xsl:when>
						<xsl:when test="text()='Test'">Test</xsl:when>
						<xsl:when test="text()='Draft'">Test</xsl:when>
					</xsl:choose>
			</distributionStatus>
  	</xsl:template>

  	<xsl:template match="rss/channel/item/cap:alert/cap:msgType">
			<distributionType>
					<xsl:choose>
						<xsl:when test="text()='Alert'">Report</xsl:when>
						<xsl:when test="text()='Update'">Update</xsl:when>
						<xsl:when test="text()='Cancel'">Cancel</xsl:when>
						<xsl:when test="text()='Ack'">Ack</xsl:when>
						<xsl:when test="text()='Error'">Error</xsl:when>
					</xsl:choose>
			</distributionType>
  	</xsl:template>

  	<xsl:template match="rss/channel/item/cap:alert/cap:info/cap:area">
		<targetArea>
			<xsl:if test="cap:circle">
				<circle><xsl:value-of select="cap:circle"/></circle>
			</xsl:if>
			<xsl:if test="cap:polygon">
				<polygon><xsl:value-of select="cap:polygon"/></polygon>
			</xsl:if>
			</targetArea>
  	</xsl:template>

  	<xsl:template match="cap:sent">
		<sent xmlns="urn:oasis:names:tc:emergency:cap:1.1">
			<xsl:value-of select="." />
		</sent>
  	</xsl:template>

  	<xsl:template match="rss/channel/item/cap:alert/cap:info/cap:parameter">	
		<xsl:choose>
			<xsl:when test="position() = last()">
				<xsl:copy-of select="."/>
					<parameter xmlns="urn:oasis:names:tc:emergency:cap:1.1">
						<valueName>eventType</valueName>
						<value><xsl:value-of select="parent::cap:info/parent::cap:alert/parent::*/gdas:eventType"/></value>
					</parameter>
					<parameter xmlns="urn:oasis:names:tc:emergency:cap:1.1">
						<valueName>alertLevel</valueName>
						<value><xsl:value-of select="parent::cap:info/parent::cap:alert/parent::*/gdas:alertLevel"/></value>
					</parameter>
			</xsl:when>
			<xsl:otherwise><xsl:copy-of select="."/></xsl:otherwise>
		</xsl:choose>		
  	</xsl:template>

  	<xsl:template match="rss/channel/item">		
		<contentObject>
			<contentDescription>
			   <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>  
				<xsl:value-of select="description"/>
			   <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>  
			</contentDescription>
			<xmlContent>
					<embeddedXMLContent>				
						<xsl:for-each select="cap:alert">									
							<alert xmlns="urn:oasis:names:tc:emergency:cap:1.1">
								<xsl:copy-of select="cap:identifier"/>						
								<xsl:copy-of select="cap:sender"/>
								<xsl:apply-templates select="cap:sent"/>
								<xsl:copy-of select="cap:status"/>
								<xsl:copy-of select="cap:msgType"/>
								<xsl:copy-of select="cap:scope"/>
								<xsl:copy-of select="cap:incidents"/>
								<info>
									<xsl:copy-of select="cap:info/cap:category"/>
									<xsl:copy-of select="cap:info/cap:event"/>
									<xsl:copy-of select="cap:info/cap:urgency"/>
									<xsl:copy-of select="cap:info/cap:severity"/>
									<xsl:copy-of select="cap:info/cap:certainty"/>
									<xsl:copy-of select="cap:info/cap:senderName"/>
									<xsl:copy-of select="cap:info/cap:headline"/>
									<xsl:copy-of select="cap:info/cap:description"/>
									<xsl:copy-of select="cap:info/cap:web"/>
									<xsl:apply-templates select="cap:info/cap:parameter"/>
									<xsl:copy-of select="cap:info/cap:area"/>
								</info>
							</alert>
						</xsl:for-each>
				</embeddedXMLContent>
			</xmlContent>
		</contentObject>
  	</xsl:template>

	<xsl:template name="FormatDate">
		<xsl:param name="DateTime" /><!-- new date format 2006-01-14T08:55:22 -->
		<xsl:variable name="day-temp">
			<xsl:value-of select="substring-after($DateTime,' ')" />
		</xsl:variable>
		<xsl:variable name="day">
			<xsl:value-of select="substring-before($day-temp,' ')" />
		</xsl:variable>
		<xsl:variable name="mo-temp">
			<xsl:value-of select="substring-after($day-temp,' ')" />
		</xsl:variable>
		<xsl:variable name="mo">
			<xsl:value-of select="substring-before($mo-temp,' ')" />
		</xsl:variable>
		<xsl:variable name="year-temp">
			<xsl:value-of select="substring-after($mo-temp,' ')" />
		</xsl:variable>
		<xsl:variable name="year">
			<xsl:value-of select="substring($year-temp,1,4)" />
		</xsl:variable>
		<xsl:variable name="time">
			<xsl:value-of select="substring-after($year-temp,' ')" />
		</xsl:variable>
		<xsl:variable name="hh">
			<xsl:value-of select="substring($time,1,2)" />
		</xsl:variable>
		<xsl:variable name="mm">
			<xsl:value-of select="substring($time,4,2)" />
		</xsl:variable>
		<xsl:variable name="zone-temp">
			<xsl:value-of select="substring-after($time,' ')" />
		</xsl:variable>
		<xsl:variable name="zone">
			<xsl:value-of select="substring($zone-temp,1,3)" />
		</xsl:variable>
		<xsl:variable name="digit">
			<xsl:value-of select="substring-after($zone-temp,$zone)" />
		</xsl:variable>			
		

		<xsl:value-of select="$year" />
		<xsl:value-of select="'-'" />
		<xsl:choose>
			<xsl:when test="$mo = 'Jan'">01</xsl:when>
			<xsl:when test="$mo = 'Feb'">02</xsl:when>
			<xsl:when test="$mo = 'Mar'">03</xsl:when>
			<xsl:when test="$mo = 'Apr'">04</xsl:when>
			<xsl:when test="$mo = 'May'">05</xsl:when>
			<xsl:when test="$mo = 'Jun'">06</xsl:when>
			<xsl:when test="$mo = 'Jul'">07</xsl:when>
			<xsl:when test="$mo = 'Aug'">08</xsl:when>
			<xsl:when test="$mo = 'Sep'">09</xsl:when>
			<xsl:when test="$mo = 'Oct'">10</xsl:when>
			<xsl:when test="$mo = 'Nov'">11</xsl:when>
			<xsl:when test="$mo = 'Dec'">12</xsl:when>
		</xsl:choose>
		<xsl:value-of select="'-'" />
		<xsl:if test="(string-length($day) &lt; 2)">
			<xsl:value-of select="0" />
		</xsl:if>
		<xsl:value-of select="$day" />
		<xsl:value-of select="'T'" />
		<xsl:value-of select="$hh" />
		<xsl:value-of select="':'" />
		<xsl:value-of select="$mm" />
		<xsl:value-of select="':00'" />
		<xsl:if test="(string-length($digit) &gt; 0)">
			<xsl:value-of select="substring($digit,1,1)" />
			<xsl:value-of select="format-number(substring($digit,2),'00')" />
			<xsl:value-of select="':00'" />
		</xsl:if>		
		<xsl:if test="(string-length($digit) = 0)">+00:00</xsl:if>
	</xsl:template>
</xsl:stylesheet>
